﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeChat.Common;
using WeChat.Common2;
using WeChat.HyfPay.Hyflib;
using WeChat.JsSDK.lib;

namespace WeChat.HyfPay
{
    public partial class PayPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region //打印请求头
                var coll = Request.Headers;
                String[] arr1 = coll.AllKeys;
                JObject jo = new JObject();
                for (int i = 0; i < arr1.Length; i++)
                {
                    String[] arr2 = coll.GetValues(arr1[i]);
                    for (int j = 0; j < arr2.Length; j++)
                    {
                        jo.Add(arr1[i], Server.HtmlEncode(arr2[j]));
                    }
                }
                ApplicationLog.Write("二维码集成页面获取请求headers：" + jo.ToString());
                #endregion

                //判断请求头，是微信app还是支付宝app提交的请求
                string agent = Request.Headers["User-Agent"].ToString();
                string url = "";
                string order_No = Request.QueryString["order_No"].ToString();
                string amount = Request.QueryString["amount"].ToString();
                string cardID = Request.QueryString["cardID"].ToString();
                //如果请求头判断为微信app请求则调用微信二维码下单接口
                if (agent.IndexOf("MicroMessenger") > -1)
                {
                  //  url = HyfPayApi.getResult("011", order_No + getrandom(), amount, cardID);
                }
                //如果请求头判断为支付宝app请求则调用支付宝二维码下单接口
                else if (agent.IndexOf("AlipayClient") > -1)
                {
                   // url = HyfPayApi.getResult("009", order_No, amount, cardID);
                }

                ApplicationLog.Write("获取二维码返回值并重定向url：" + url);
                if(url!="")
                Response.Redirect(url);
            }
            
        }  
        public string getrandom()
        {
            Random rd = new Random();
            int num = rd.Next(10, 99);
            return num.ToString();
        }
    }
}